<template>
  <layout>
    <el-form >
    {{$route.params.id}}
      <el-form-item label="昵称">
       <el-input  placeholder="" v-model='form.nickname'></el-input>
      </el-form-item>
      <el-form-item label="账户">
       <el-input  placeholder="" v-model='form.username' :readonly="$route.params.id" ></el-input>
      </el-form-item>
      <el-form-item label="密码">
       <el-input  placeholder="" v-model='form.password'></el-input>
      </el-form-item>
      <el-form-item >
        <el-button @click='save'>保存</el-button>
      </el-form-item>
    </el-form>
    
  </layout>
</template>

<script>
export default {
  data() {
    return {
      form:{
        username:'',
        
      }
    }
  },
  created() {
    //编辑的话才获取相应ID的数据
    if(this.$route.params.id){
      this.$http.get(`/role/get?id=${this.$route.params.id}`).then( res =>{
      if(res.errcode == 0){
        this.form = res.data
      }
      
    })
    }
    
  },
  methods: {
    save(){
      //有ID就是编辑
      if(this.$route.params.id){
              this.$http.post('/cate/update',this.form).then( res => {
        if(res.errcode == 0){
          this.$router.back()
        }
      })
      }
      else{
      this.$http.post('/cate/insert',this.form).then( res => {
        if(res.errcode == 0){
          this.$router.back()
        }
      })
      }

    }
  },
}
</script>

<style >
  
</style>